# -*- coding: utf-8 -*-
# @Time    : 2018/9/28 9:42
# 主要对库中的关键词扩展

import os
import sys
path = os.path.abspath(os.path.dirname(os.getcwd()))
sys.path.append(path)
import ast
import requests
from mongoFild import insert_mongo
from lxml.html import etree

headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.12 Safari/537.36',
        }

class baidu_Key_Mongo(object):
    def __init__(self) -> None:
        super().__init__()


    def find_url(self, word):
        data = {
            'pt':'page.serp',
            'bq':'%s'%(word),
            'mkt':'zh-cn',
            'qry':'%s'%(word),
            'cp':'3',
            'css':'1',
            'cvid':'C0BB80FECE4146D2A7B99A6FBBFDF380',
        }
        try:
            req = requests.get('https://cn.bing.com/AS/Suggestions', params=data, headers=headers,timeout=120)
            req.close()
            con_et = etree.HTML(req.text)
            words = con_et.xpath('//li[@class="sa_sg"]/@query')
            print("jiansuo_key len(): %s  keyword:%s " % (len(words), data['bq']))
            for i in words:
                insert_mongo().insert_mongo_word(i)
        except Exception as e:
            print(e)


if __name__ == '__main__':
    baidu_Key_Mongo().find_url("你好")
